﻿using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infastruucture;
using BasicData.Read.API.application.Command.User;
using MediatR;

namespace BasicData.Read.API.application.CommandHandler.User
{
    public class UserQueryCommandHandler : IRequestHandler<UserQueryCommand, ApiResult<List<UserModel>>>
    {
        private readonly IBaseRepository<UserModel> userRep;

        public UserQueryCommandHandler(IBaseRepository<UserModel> userRep)
        {
            this.userRep = userRep;
        }
        public Task<ApiResult<List<UserModel>>> Handle(UserQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<UserModel>> result = new ApiResult<List<UserModel>>();
            result.Code = BaseEnums.Success;
            result.Msg = "用户列表查询成功";
            var list = userRep.GetAll();
            if(!string.IsNullOrEmpty(request.UserNickName))
            {
                list=list.Where(x=>x.UserNickName.Contains(request.UserNickName));
            }
            result.Data=list.ToList();
            return Task.FromResult(result);
        }
    }
}
